package com.zjdiepu.www.proxy.biz.impl.transfer;

import java.util.List;
import java.util.Set;

import org.apache.commons.collections.CollectionUtils;
import org.apache.commons.lang.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.zjdiepu.www.base.core.BaseException;
import com.zjdiepu.www.base.core.BaseService;
import com.zjdiepu.www.base.enums.type.OperateType;
import com.zjdiepu.www.base.enums.type.ResultFlagType;
import com.zjdiepu.www.proxy.model.ext.transfer.TransferOtherTimedTaskExt;
import com.zjdiepu.www.proxy.dao.transfer.TransferOtherTimedTaskMapper;
import com.zjdiepu.www.proxy.model.dto.transfer.TransferOtherTimedTask;
import com.zjdiepu.www.proxy.biz.transfer.ITransferOtherTimedTaskService;

/**
 * 中转_other_定时任务Service层操作实现<br/>
 * @author xiongxiaotun<br/>
 * @time 2017年12月13日 17时50分<br/>
 * @since 1.0<br/>
 */
@Service
public class TransferOtherTimedTaskServiceImpl extends BaseService implements ITransferOtherTimedTaskService {
	public static final Logger logger = LoggerFactory.getLogger(TransferOtherTimedTaskServiceImpl.class);

	@Autowired
	private TransferOtherTimedTaskMapper transferOtherTimedTaskMapper;
	
	@Override
	public void saveUpdateAndDelete(TransferOtherTimedTask transferOtherTimedTask, OperateType operType) {
		switch (operType) {
		case add:
			transferOtherTimedTask.initInsertData(transferOtherTimedTask.getModifier(), transferOtherTimedTask.getModifierName());
			transferOtherTimedTaskMapper.insert(transferOtherTimedTask);
			break;
		case edit:
			transferOtherTimedTask.initUpdateData(transferOtherTimedTask.getModifier(), transferOtherTimedTask.getModifierName());
			// 影响的记录数
			int updateCount = transferOtherTimedTaskMapper.updateByPrimaryKeySelective(transferOtherTimedTask);
			if(updateCount == 0){
				throw new BaseException(ResultFlagType.MISSING.getCode().toString(),ResultFlagType.MISSING.getValue());
			} else if(updateCount > 1){
				throw new BaseException(ResultFlagType.MULTI_DATA.getCode().toString(),ResultFlagType.MULTI_DATA.getValue());
			}
			break;
		case delete:
			transferOtherTimedTask.initDeleteData(transferOtherTimedTask.getModifier(), transferOtherTimedTask.getModifierName());
			// 影响的记录数
			int deleteCount = transferOtherTimedTaskMapper.updateByPrimaryKeySelective(transferOtherTimedTask);
			if(deleteCount == 0){
				throw new BaseException(ResultFlagType.MISSING.getCode().toString(),ResultFlagType.MISSING.getValue());
			} else if(deleteCount > 1){
				throw new BaseException(ResultFlagType.MULTI_DATA.getCode().toString(),ResultFlagType.MULTI_DATA.getValue());
			}
			break;
		default:
			break;
		}
	}
	
	@Override
	public List<TransferOtherTimedTask> selectByExt(TransferOtherTimedTaskExt transferOtherTimedTaskExt) {
		return transferOtherTimedTaskMapper.selectByExt(transferOtherTimedTaskExt);
	}
	
	@Override
	public int countByExt(TransferOtherTimedTaskExt transferOtherTimedTaskExt){
		return transferOtherTimedTaskMapper.countByExt(transferOtherTimedTaskExt);
	}
	
	@Override
	public TransferOtherTimedTask selectById(String id) {
		return transferOtherTimedTaskMapper.selectByPrimaryKey(id);
	}
	
	@Override
	public void clearEmpty(Set<String> fieldNames, String id) {
		if(CollectionUtils.isNotEmpty(fieldNames)){
			transferOtherTimedTaskMapper.clearEmpty(fieldNames, id);
		}
	}
	
	@Override
	public int countRefined(TransferOtherTimedTask transferOtherTimedTask) {
		return transferOtherTimedTaskMapper.countRefined(transferOtherTimedTask);
	}

	@Override
	public TransferOtherTimedTask selectRefined(TransferOtherTimedTask transferOtherTimedTask) {
		return transferOtherTimedTaskMapper.selectRefined(transferOtherTimedTask);
	}
	
	@Override
	public int deletedRefined(TransferOtherTimedTask transferOtherTimedTask) {
		return transferOtherTimedTaskMapper.deletedRefined(transferOtherTimedTask);
	}

	@Override
	public boolean repeat(TransferOtherTimedTask transferOtherTimedTask) {
		int count = countRefined(transferOtherTimedTask);
		if(count > 1) {
			return true;
		}
		if(count == 0) {
			return false;
		}
		
		if(StringUtils.isBlank(transferOtherTimedTask.getId())) {
			return true;
		}
		TransferOtherTimedTask dbItem = selectRefined(transferOtherTimedTask);
		if(null == dbItem) {
			return false;
		}
		return !transferOtherTimedTask.getId().equals(dbItem.getId());
	}
	
	@Override
	public int updateBatch(String modifier, String modifierName, Integer dpStatus, List<String> ids) {
		Integer current = (int)(System.currentTimeMillis() / 1000);
		return transferOtherTimedTaskMapper.updateBatch(modifier, modifierName, current, dpStatus, ids);
	}
	
	@Override
	public int insertBatch(List<TransferOtherTimedTask> transferOtherTimedTasks) {
		return transferOtherTimedTaskMapper.insertBatch(transferOtherTimedTasks);
	}
}
